package requrest

import (
	"github.com/gin-gonic/gin"
	"go-project/dto"
	"strconv"

	"errors"
	"go-project/models"
)

type PermissionsSearchRequest struct {
	Pagination Pagination
	Name       string `form:"name"`
}
type PermissionsInsertRequest struct {
	ParentID    string ` json:"parent_id" binding:"required" required_msg:"上级菜单不能为空"`
	IsMenu      int8   ` json:"is_menu" binding:"required" required_msg:"是否是菜单不能为空"`
	Action      string `json:"action" binding:"required" required_msg:"访问方法不能为空"`
	Sort        int8   ` json:"sort"`
	MenuUrl     string ` json:"menu_url" binding:"required" required_msg:"菜单url不能为空"`
	Icon        string ` json:"icon"`
	Code        string `json:"code" binding:"required,permissions_code_unique" required_msg:"权限标识不能为空" permissions_code_unique_msg:"该权限标识已经存在"`
	DisplayName string ` json:"display_name" binding:"required,permissions_name_unique" required_msg:"权限名称不能为空" permissions_name_unique_msg:"该权限名称已经存在" `
	dto.ValidateBindRequest
}

func (articleInsertRequest *PermissionsInsertRequest) BindAndValidate(c *gin.Context) error {
	if err := c.ShouldBindJSON(articleInsertRequest); err != nil {

		c.Errors = append(c.Errors, c.Error(err).SetType(gin.ErrorTypeBind))
		c.Set("req_dto", articleInsertRequest)
		return err
	}
	return nil
}

func (permissionsInsertRequest *PermissionsInsertRequest) Generate(data interface{}) *dto.ValidateBindRequest {
	permissions, ok := data.(*models.Permissions)
	if ok {
		atoi, _ := strconv.Atoi(permissionsInsertRequest.ParentID)
		if atoi != 0 {
			u := uint(atoi)
			permissions.ParentID = &u
		} else {
			permissions.ParentID = nil
		}
		permissions.Icon = permissionsInsertRequest.Icon
		permissions.Action = permissionsInsertRequest.Action
		permissions.Url = permissionsInsertRequest.MenuUrl
		permissions.DisplayName = permissionsInsertRequest.DisplayName
		permissions.Code = permissionsInsertRequest.Code
		permissions.IsMenu = permissionsInsertRequest.IsMenu
		permissions.Sort = permissionsInsertRequest.Sort
		return nil
	}
	permissionsInsertRequest.Errors = errors.New("数据类型不正确，数据必须是models.permissions类型")
	return &permissionsInsertRequest.ValidateBindRequest

}

type PermissionsUpdateRequest struct {
	ID          uint   ` json:"id" binding:"required" required_msg:"权限id不能为空"`
	ParentID    string ` json:"parent_id" binding:"required" required_msg:"上级菜单不能为空"`
	IsMenu      int8   ` json:"is_menu" binding:"required" required_msg:"是否是菜单不能为空"`
	Sort        int8   ` json:"sort"`
	Action      string `json:"action" binding:"required" required_msg:"访问方法不能为空"`
	MenuUrl     string ` json:"menu_url" binding:"required" required_msg:"菜单url不能为空"`
	Icon        string ` json:"icon"`
	Code        string `json:"code" binding:"required,permissions_code_unique=update" required_msg:"权限标识不能为空" permissions_code_unique_msg:"该权限标识已经存在"`
	DisplayName string ` json:"display_name" binding:"required,permissions_name_unique=update" required_msg:"权限名称不能为空" permissions_name_msg:"该权限名称已经存在" `
	dto.ValidateBindRequest
}

func (permissionsUpdateRequest *PermissionsUpdateRequest) BindAndValidate(c *gin.Context) error {
	if err := c.ShouldBindJSON(permissionsUpdateRequest); err != nil {
		c.Errors = append(c.Errors, c.Error(err).SetType(gin.ErrorTypeBind))
		c.Set("req_dto", permissionsUpdateRequest)
		return err
	}
	return nil
}

func (permissionsUpdateRequest *PermissionsUpdateRequest) Generate(data interface{}) *dto.ValidateBindRequest {
	permissions, ok := data.(*models.Permissions)
	if ok {
		atoi, _ := strconv.Atoi(permissionsUpdateRequest.ParentID)
		permissions.ID = permissionsUpdateRequest.ID
		permissions.Action = permissionsUpdateRequest.Action
		if atoi != 0 {
			u := uint(atoi)
			permissions.ParentID = &u
		} else {
			permissions.ParentID = nil
		}
		permissions.Icon = permissionsUpdateRequest.Icon
		permissions.Url = permissionsUpdateRequest.MenuUrl
		permissions.DisplayName = permissionsUpdateRequest.DisplayName
		permissions.Code = permissionsUpdateRequest.Code
		permissions.IsMenu = permissionsUpdateRequest.IsMenu
		permissions.Sort = permissionsUpdateRequest.Sort
		return nil
	}
	permissionsUpdateRequest.Errors = errors.New("数据类型不正确，数据必须是models.permissions类型")
	return &permissionsUpdateRequest.ValidateBindRequest

}
